#{extends 'adminm/frame.html' /}

#{set 'moreScripts'}
<script src="@{'/public/javascripts/jquery-ui-1.8.10.custom.min.js'}" type="text/javascript" charset="utf-8"></script>
<script src="@{'/public/pager/jquery.pager.js'}" type="text/javascript" charset="utf-8"></script>
#{/set}
#{set 'moreStyles'}
<link rel="stylesheet" type="text/css" media="screen" href="@{'/public/pager/Pager.css'}" />
<link rel="stylesheet" type="text/css" media="screen" href="@{'/public/stylesheets/table.scss'}" />
#{/set}
#{set 'activeItem'}7#{/set}

<div class="navigationBar">
	<table>
		<tr>
			<td><img src="@{'/public/images/navigationHouse.gif'}"></td>
			<td><strong>当前位置：</strong></td>
			<td>
				<a href="/admin">首页</a>
				>>
				<a href="#">运价管理</a>
				>>
				<font class="current">价格管理</font>
			</td>
		</tr>
	</table>
</div>

<script language="javascript">

	$(
		function(){
			$("#starProvinceId").change(function(){
				var id = $("#starProvinceId").val();
				searchCityByProvince(id,"#starCityId");
			 });

			$("#endProvinceId").change(function(){
				var id = $("#endProvinceId").val();
				searchCityByProvince(id,"#endCityId");
			 });

			$("#starProvinceId-create").change(function(){
				var id = $("#starProvinceId-create").val();
				searchCityByProvince(id,"#starCityId-create");
			 });

			$("#endProvinceId-create").change(function(){
				var id = $("#endProvinceId-create").val();
				searchCityByProvince(id,"#endCityId-create");
			 });

			//初始化创建窗口
			$("#create-form").dialog({autoOpen:false,modal:true,
	        buttons:{提交:function(){createTransPrice();}}
				})

			//初始化更新窗口
			$("#update-form").dialog({autoOpen:false,modal:true,
	        buttons:{提交:function(){updateTransPrice();}}
				})

		}
	);

	$(function(){
		//初始化翻页控件。
		$("#pager").pager(
			{
				pagenumber: ${queryBean.currentPage}, 
				pagecount: ${queryBean.totalPage}, 
				totalcount: ${queryBean.totalQuantity},
				buttonClickCallback: PageClick 
			}
		);
	});
	//翻页功能。
	PageClick = function(pageclickednumber) {
		$("#currentPage").val(pageclickednumber);
		doSearch();
	}

	//打开创建窗口
	function openCreateDialog(){
		$("#create-form").dialog({width:400});
       $("#create-form").dialog("open");
	}

	//打开修改窗口
	function openUpdateDialog(id){

		$("#transprice-update").val(id);
		
		$.ajax({
			url: "@{admin.TransPrices.getTransPrice()}",
			data: "transPriceId="+id,
			type: "GET",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				//获取返回内容并进行解析
				var content = XMLHttpRequest.responseText;
				var result = content.split("#");
				for(var i=0;i<result.length;i++)
				{
					var priceResult = result[i].split(":");
					$("#"+priceResult[0]).val(priceResult[1]);
				}
			}
		});

	   $("#update-form").dialog({width:400});
       $("#update-form").dialog("open");
	}

	//刷新页面
	function doSearch() {
		$("#form1").submit();
	}

	//分页查询提交
	function doSearchByPage() {
		var companyVal = $("companyId").val();
		var startProvVal = $("starProvinceId").val();
		var startCityVal = $("starCityId").val();
		var endProvVal = $("endProvinceId").val();
		var endCityVal = $("endCityId").val();

		$("#companyId-page").val(companyVal);
		$("#starProvinceId-page").val(startProvVal);
		$("#starCityId-page").val(startCityVal);
		$("#endProvinceId-page").val(endProvVal);
		$("#endCityId-page").val(endCityVal);
		
		$("#form2").submit();
	}

	//异步加载城市列表
	function searchCityByProvince(id,selectorId){

		var dataStr = "provid="+id;
		
		$.ajax({
			url: "@{admin.TransPrices.getCityListByProvince()}",
			data: dataStr,
			type: "GET",
			dataType: "json",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				//将原来下拉列表的内容清除
				$(selectorId+" option").remove();
				$(selectorId).append("<option value='NULL'>请选择</option>");
				if(textStatus == "error") {
					var errJson = eval("("+XMLHttpRequest.responseText+")");
					var errMsg = "查询城市列表失败"+errJson.message;
					alert(errMsg);
				} else {
					//获取返回内容并进行解析
					var content = XMLHttpRequest.responseText;
					var result = content.split("#");
					for(var i=0;i<result.length-1;i++){
						var cityResult = result[i].split("|");
					$(selectorId).append("<option value='"+cityResult[0]+"'>"+cityResult[1]+"</option>")
					}
				}
			}
		});
	}

	//提交创建信息
	function createTransPrice() {
		var companyIdVal = $("#companyId-create").val();
		var startProvIdVal= $("#starProvinceId-create").val();
		var startCityIdVal= $("#starCityId-create").val();
		var endProvIdVal= $("#endProvinceId-create").val();
		var endCityIdVal= $("#endCityId-create").val();
		var publicBasePriceVal= $("#publicBasePrice-create").val();
		var publicRenewalPriceVal= $("#publicRenewalPrice-create").val();
		var privateBasePriceVal= $("#privateBasePrice-create").val();
		var privateRenewalPriceVal= $("#privateRenewalPrice-create").val();
		var publicBaseWeightVal = $("#publicBaseWeight-create").val();
		var privateBaseWeightVal = $("#privateBaseWeight-create").val();

		//必填输入校验
		var req1 = required("#publicBasePrice-create" , "公有起价必填");
		var req2 = required("#publicRenewalPrice-create" , "公有续价必填");
		var req3 = required("#privateBasePrice-create" , "私有起价必填");
		var req4 = required("#privateRenewalPrice-create" , "私有续价必填");
		var req5 = required("#publicBaseWeight-create" , "公有基重必填");
		var req6 = required("#privateBaseWeight-create" , "私有基重必填");

		if(!(req1 && req2 && req3 && req4 && req5 && req6)){
			return false;
		}
		
		//数据格式验证
		var msg = "输入的数值必须为整数或者小数,小数最多为两位";
		var result1 = regex("#publicBasePrice-create" , /^\d+(.\d{0,2})?$/);
		var result2 = regex("#publicRenewalPrice-create" , /^\d+(.\d{0,2})?$/);
		var result3 = regex("#privateBasePrice-create" , /^\d+(.\d{0,2})?$/);
		var result4 = regex("#privateRenewalPrice-create" , /^\d+(.\d{0,2})?$/);
		var result5 = regex("#publicBaseWeight-create" , /^\d+(.\d{0,2})?$/);
		var result6 = regex("#privateBaseWeight-create" , /^\d+(.\d{0,2})?$/);
		
		if(!(result1 && result2 && result3 && result4 && result5 && result6)){
			alert(msg);
			return false;
		}

		var dataStr = "companyIdVal="+companyIdVal;
		dataStr = dataStr+"&startProvIdVal="+startProvIdVal;
		dataStr = dataStr+"&startCityIdVal="+startCityIdVal;
		dataStr = dataStr+"&endProvIdVal="+endProvIdVal;
		dataStr = dataStr+"&endCityIdVal="+endCityIdVal;
		dataStr = dataStr+"&publicBasePriceVal="+publicBasePriceVal;
		dataStr = dataStr+"&publicRenewalPriceVal="+publicRenewalPriceVal;
		dataStr = dataStr+"&privateBasePriceVal="+privateBasePriceVal;
		dataStr = dataStr+"&privateRenewalPriceVal="+privateRenewalPriceVal;
		dataStr = dataStr+"&publicBaseWeight="+publicBaseWeightVal;
		dataStr = dataStr+"&privateBaseWeight="+privateBaseWeightVal;
		
		$.ajax({
			url: "@{admin.TransPrices.createTransPrice()}",
			data: dataStr,
			type: "GET",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				//获取返回内容并进行解析
				var content = XMLHttpRequest.responseText;
				if("DUPLICATE" == content){
					alert("运价重复，该公司的起始城市之间的运价已经被设置");
				}else{
					alert("运价创建成功");
					doSearch();
				}
			}
		});
	}

	//提交修改信息
	function updateTransPrice() {
		var transPriceId =$("#transprice-update").val();
		var publicBasePriceVal= $("#publicBasePrice-update").val();
		var publicRenewalPriceVal= $("#publicRenewalPrice-update").val();
		var privateBasePriceVal= $("#privateBasePrice-update").val();
		var privateRenewalPriceVal= $("#privateRenewalPrice-update").val();
		var publicBaseWeightVal = $("#publicBaseWeight-update").val();
		var privateBaseWeightVal = $("#privateBaseWeight-update").val();

		//必填输入校验
		var req1 = required("#publicBasePrice-update" , "公有起价必填");
		var req2 = required("#publicRenewalPrice-update" , "公有续价必填");
		var req3 = required("#privateBasePrice-update" , "私有起价必填");
		var req4 = required("#privateRenewalPrice-update" , "私有续价必填");
		var req5 = required("#publicBaseWeight-update" , "公有基重必填");
		var req6 = required("#privateBaseWeight-update" , "私有基重必填");

		if(!(req1 && req2 && req3 && req4 && req5 && req6)){
			return false;
		}
		
		//数据格式验证
		var msg = "输入的数值必须为整数或者小数,小数最多为两位";
		var result1 = regex("#publicBasePrice-update" , /^\d+(.\d{0,2})?$/);
		var result2 = regex("#publicRenewalPrice-update" , /^\d+(.\d{0,2})?$/);
		var result3 = regex("#privateBasePrice-update" , /^\d+(.\d{0,2})?$/);
		var result4 = regex("#privateRenewalPrice-update" , /^\d+(.\d{0,2})?$/);
		var result5 = regex("#publicBaseWeight-update" , /^\d+(.\d{0,2})?$/);
		var result6 = regex("#privateBaseWeight-update" , /^\d+(.\d{0,2})?$/);
		
		if(!(result1 && result2 && result3 && result4 && result5 && result6)){
			alert(msg);
			return false;
		}

		var dataStr = "transPriceId="+transPriceId;
		dataStr = dataStr+"&publicBasePriceVal="+publicBasePriceVal;
		dataStr = dataStr+"&publicRenewalPriceVal="+publicRenewalPriceVal;
		dataStr = dataStr+"&privateBasePriceVal="+privateBasePriceVal;
		dataStr = dataStr+"&privateRenewalPriceVal="+privateRenewalPriceVal;
		dataStr = dataStr+"&publicBaseWeight="+publicBaseWeightVal;
		dataStr = dataStr+"&privateBaseWeight="+privateBaseWeightVal;

		$.ajax({
			url: "@{admin.TransPrices.updateTransPrice()}",
			data: dataStr,
			type: "GET",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				//获取返回内容并进行解析
				var content = XMLHttpRequest.responseText;
				if("SUCCESS" == content){
					alert("更新成功");
					doSearch();
				}else{
					alert("更新失败");
				}
			}
		});
	}

	//正则表达式校验
	var regex = function(field,regex)
	{
		var targetValue = $(field).val();
		if (targetValue == null || !regex.test(targetValue))
		{
			return false;
		}else{
			return true;
		}
	}

	//必填校验
	var required = function(field ,errTip)
	{
		var targetValue = $(field).val();
		if (targetValue == null || targetValue == "")
		{
			alert(errTip);
			return false;	
		}
		else
		{
			return true;
		}
	}

	//删除 运价
	function removeItem(id) {

		var result = confirm("确实要 删除吗?")
		if(result == false){
			return false;
		}  
		
		$.ajax({
			url: "@{admin.TransPrices.deleteTransPrice()}",
			data: "transPriceId="+id,
			type: "GET",
			complete: function(XMLHttpRequest, textStatus, errorThrown) {
				//获取返回内容并进行解析
				var content = XMLHttpRequest.responseText;
				if("SUCCESS" == content){
					alert("删除成功");
					doSearch();
				}else{
					alert("删除失败");
				}
			}
		});
	}
	
</script>

<div>
	<div class="list_criteria">
		#{form @admin.TransPrices.queryTransPrice(), name:'form1', id:'form1'}
		运输公司:
		<select name="companyId" id="companyId">
	                <option value="NULL">请选择</option>
					#{if companyList}
	             		#{list items:companyList, as:'company'}
	             			 <option value="${company.id}">${company.companyName}</option>
	             		#{/list}
	                #{/if}
	     </select>
	     
	     始发省份:
	   <select name="starProvinceId" id="starProvinceId">
              <option value="NULL">请选择</option>
				#{if provinceList}
	           		#{list items:provinceList, as:'province'}
	           			 <option value="${province.id}">${province.name}</option>
	           		#{/list}
              	#{/if}
		</select>
	    
	    始发城市:
	      <select name="starCityId" id="starCityId">
	                <option value="NULL">请选择</option>
	      </select>
	           
	    目的省份:     
	      <select name="endProvinceId" id="endProvinceId">
               <option value="NULL">请选择</option>
			#{if provinceList}
            		#{list items:provinceList, as:'province'}
            			 <option value="${province.id}">${province.name}</option>
            		#{/list}
               #{/if}
	      </select>
	        
	      目的城市:        
           <select name="endCityId" id="endCityId">
               <option value="NULL">请选择</option>
           </select>&nbsp;&nbsp;
           
      <input type="hidden" id="currentPage" name="queryBean.currentPage" value="${queryBean.currentPage}">
	每页
	<select name="queryBean.quantityEachPage" onchange="doSearch()">
		<option value="2" #{if queryBean.quantityEachPage==2} selected #{/if}>2</option>
		<option value="10" #{if queryBean.quantityEachPage==10} selected #{/if}>10</option>
		<option value="25" #{if queryBean.quantityEachPage==25} selected #{/if}>25</option>
		<option value="50" #{if queryBean.quantityEachPage==50} selected #{/if}>50</option>
	</select>
	条记录<br/><br/>
           <input type="submit" value="查询">&nbsp;
	<input type="button" value="添加" onclick="javascript:openCreateDialog();">    
    #{/form}
</div>

<div>
	#{if priceList}  
	<table class="common-table">
		<tr>
			<td>快递公司</td>
			<td>始发省份</td>
			<td>始发城市</td>
			<td>目的省份</td>
			<td>目的城市</td>
			<td>公有起价</td>
			<td>公有续价</td>
			<td>私有起价</td>
			<td>私有续价</td>
			<td>公有起始重量</td>
			<td>私有起始重量</td>
			<td>操作</td>
		</tr>
		#{list items:priceList, as:'priceItem'}
		<tr>
			<td>${priceItem.company.companyName}</td>
			<td>${priceItem.startCity.province.name}</td>
			<td>${priceItem.startCity.cityName}</td>
			<td>${priceItem.endCity.province.name}</td>
			<td>${priceItem.endCity.cityName}</td>
			<td>${priceItem.publicBasePrice}</td>
			<td>${priceItem.publicRenewalPrice}</td>
			<td>${priceItem.privateBasePrice}</td>
			<td>${priceItem.privateRenewalPrice}</td>
			<td>${priceItem.publicBaseWeight}</td>
			<td>${priceItem.privateBaseWeight}</td>
			<td>
				<a class="editer" href="javascript:openUpdateDialog(${priceItem.id})">[修改]</a>&nbsp;
				<a class="editer" href="javascript:removeItem(${priceItem.id})">[删除]</a>&nbsp;
			</td>
		</tr>  		
		#{/list}
	</table>			
	#{/if}
</div>

<!-- 创建窗口 -->
<div id="create-form" class="ui-widget ui-widget-content ui-corner-all" title="创建运价">
    <form id="create-form">
    	<div class="list_criteria">
			运输公司：
			<select name="companyId-create" id="companyId-create">
	                <option value="NULL">请选择</option>
					#{if companyList}
	             		#{list items:companyList, as:'company'}
	             			 <option value="${company.id}">${company.companyName}</option>
	             		#{/list}
	                #{/if}
	         </select><br/><br/>
		    始发省份：
		    <select name="starProvinceId-create" id="starProvinceId-create">
	                <option value="NULL">请选择</option>
					#{if provinceList}
	             		#{list items:provinceList, as:'province'}
	             			 <option value="${province.id}">${province.name}</option>
	             		#{/list}
	                #{/if}
	                </select>&nbsp;&nbsp;
		    始发城市：
		     <select name="starCityId-create" id="starCityId-create">
	                <option value="NULL">请选择</option>
	                </select><br/><br/>
		   目的省份：
		   <select name="endProvinceId-create" id="endProvinceId-create">
	                <option value="NULL">请选择</option>
					#{if provinceList}
	             		#{list items:provinceList, as:'province'}
	             			 <option value="${province.id}">${province.name}</option>
	             		#{/list}
	                #{/if}
	                </select>&nbsp;&nbsp;
		   目的城市：
		   <select name="endCityId-create" id="endCityId-create">
	                <option value="NULL">请选择</option>
	                </select><br/><br/>
		   公有起价：
		   <input type="text" name="publicBasePrice-create" id="publicBasePrice-create" size="10"/>&nbsp;&nbsp;
		  公有续价：
		   <input type="text" name="publicRenewalPrice-create" id="publicRenewalPrice-create" size="10"/><br/><br/>
		   私有起价：
		   <input type="text" name="privateBasePrice-create" id="privateBasePrice-create" size="10"/>&nbsp;&nbsp;
		   私有续价：
		   <input type="text" name="privateRenewalPrice-create" id="privateRenewalPrice-create" size="10"/><br/><br/>
		  公有基重：
		   <input type="text" name="publicBaseWeight-create" id="publicBaseWeight-create" size="10"/>&nbsp;&nbsp;
		   私有基重：
		   <input type="text" name="privateBaseWeight-create" id="privateBaseWeight-create" size="10"/>
		</div>

    </form>
</div>



<!-- 更新窗口 -->
<div id="update-form" class="ui-widget ui-widget-content ui-corner-all" title="修改运价">
	<form id="update-form">
		<div class="list_criteria">
			运输公司：
			<input type="hidden" name="transprice-update" id="transprice-update"/>
		    <input type="text" name="companyId-update" id="companyId-update" size="10" disabled=true"/><br/><br/>
		    始发省份：
		    <input type="text" name="starProvinceId-update" id="starProvinceId-update" size="10" disabled=true"/>&nbsp;&nbsp;
		    始发城市：
		    <input type="text" name="starCityId-update" id="starCityId-update" size="10" disabled=true"/><br/><br/>
		   目的省份：
		   <input type="text" name="endProvinceId-update" id="endProvinceId-update" size="10" disabled=true"/>&nbsp;&nbsp;
		   目的城市：
		   <input type="text" name="endCityId-update" id="endCityId-update" size="10" disabled=true"/><br/><br/>
		   公有起价：
		   <input type="text" name="publicBasePrice-update" id="publicBasePrice-update" size="10"/>&nbsp;&nbsp;
		  公有续价：
		   <input type="text" name="publicRenewalPrice-update" id="publicRenewalPrice-update" size="10"/><br/><br/>
		   私有起价：
		   <input type="text" name="privateBasePrice-update" id="privateBasePrice-update" size="10"/>&nbsp;&nbsp;
		   私有续价：
		   <input type="text" name="privateRenewalPrice-update" id="privateRenewalPrice-update" size="10"/><br/><br/>
		  公有基重：
		   <input type="text" name="publicBaseWeight-update" id="publicBaseWeight-update" size="10"/>&nbsp;&nbsp;
		   私有基重：
		   <input type="text" name="privateBaseWeight-update" id="privateBaseWeight-update" size="10"/>
		</div>
	</form> 
</div>

<br/>
<div id="pager" style="font-size:1.3em;"></div>
</div>
